package ee.videorentalstore.protocols.commands.changingsystemusercommands;

import java.util.List;
import ee.videorentalstore.UserAndProtocolManager;
import ee.videorentalstore.UserAndProtocolManagerStatus;
import ee.videorentalstore.protocols.Protocol;
import ee.videorentalstore.protocols.ProtocolFinder;
import ee.videorentalstore.protocols.commands.Command;
import ee.videorentalstore.users.User;
import ee.videorentalstore.users.UsersDatabase;

public class SetSystemUserByUserIdCommand extends Command
{
	private final int NEW_SYSTEM_USER_ID_POSITION = 1;
	
	
	@Override
	public String getCommandDescription() 
	{
		return "SET SYSTEM USER BY ID";
	}
	
	
	@Override
	public void executeCommand(String inputStreamInstructionsToProcess) 
	{
		List<String> splittedInstructionsList = 
				getSplittedInputStreamInstructionsToProcess(inputStreamInstructionsToProcess);
		Integer newSystemUserID = getSpecificIntegerValueOutOfSplittedInstructionsList(
				splittedInstructionsList, NEW_SYSTEM_USER_ID_POSITION);
		if (newSystemUserID != null)
		{
			boolean userExists = 
					UsersDatabase.getInstance().getWhetherUserExistsByID(newSystemUserID.intValue());
			if (userExists)
			{
				User newSystemUser = UsersDatabase.getInstance().getUser(newSystemUserID.intValue());
				UserAndProtocolManager.getInstance().setUserWhoIsInControl(newSystemUser);
				Protocol protocolToUse = ProtocolFinder.getProtocolByUserType(newSystemUser);
				UserAndProtocolManager.getInstance().setProtocolToUse(protocolToUse);
				UserAndProtocolManager.getInstance().setUserAndProtocolManagerStatus(
						UserAndProtocolManagerStatus.FOLLOWING_CURRENT_PROTOCOL);
				System.out.println("User in control has been successfully changed!");
			}
			else
			{
				System.out.println("User with given id does not exist!");
			}
		}
		else
		{
			System.out.println("Command could not be executed due to inserted instructions errors!");
		}
	}
}
